package dao

import (
	"fmt"
	"go_cms/app/model"
	"go_cms/app/param"
	"go_cms/utils"
)

type GoodsDao struct {
	*utils.Db
}

// 获取商品列表信息
func (this *GoodsDao)GoodsList(queryInfo param.QueryInfo) ([]*param.Goods, int64){
	var goods []*param.Goods
	this.Table("goods").Limit(queryInfo.Size).Offset(queryInfo.Offset).Find(&goods)
	//this.Table("users").Find(&users)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",goods[0])
	}
	var count int64
	this.Table("goods").Count(&count)
	return goods,count
}

// 获取商品分类数量
func (this *GoodsDao)CategoryCount() ([]*param.CategoryCount){
	var categoryCount []*param.CategoryCount
	sqlStr:="select categories.id,categories.name,(select count(goods.categoryId) from goods where goods.categoryId=categories.id) as 'GoodsCount' from categories"
	this.Raw(sqlStr).Scan(&categoryCount)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",categoryCount[0])
	}
	return categoryCount
}

// 获取商品分类销量
func (this *GoodsDao)CategorySale() ([]*param.CategoryCount){
	var categoryCount []*param.CategoryCount
	sqlStr:="select categories.id,categories.name,(select sum(goods.saleCount) from goods where goods.categoryId=categories.id) as 'GoodsSale' from categories"
	this.Raw(sqlStr).Scan(&categoryCount)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",categoryCount[0])
	}
	return categoryCount
}

// 获取商品分类收藏
func (this *GoodsDao)CategoryFavor() ([]*param.CategoryCount){
	var categoryCount []*param.CategoryCount
	sqlStr:="select categories.id,categories.name,(select sum(goods.favorCount) from goods where goods.categoryId=categories.id) as 'GoodsFavor' from categories"
	this.Raw(sqlStr).Scan(&categoryCount)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",categoryCount[0])
	}
	return categoryCount
}

// 获取商品地区销量
func (this *GoodsDao)AddressSale() ([]*param.AddressSale){
	var categoryCount []*param.AddressSale
	sqlStr:="SELECT goods.address,SUM(goods.saleCount) as 'Count' from goods GROUP BY goods.address ORDER BY 'Count' DESC"
	this.Raw(sqlStr).Scan(&categoryCount)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",categoryCount[0])
	}
	return categoryCount
}

// 获取各分类数量统计列表
func (this *GoodsDao)AmountList() ([]*param.AmountList){
	var amountList []*param.AmountList
	var amounts param.Amounts
	sqlStr:="SELECT SUM(goods.saleCount*goods.newPrice) as 'SaleTotalCount',SUM(goods.inventoryCount) as 'InventoryCount',SUM(goods.favorCount) as 'FavorCount',SUM(goods.saleCount) as 'SaleCount' from goods"
	this.Raw(sqlStr).Scan(&amounts)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",amounts)
	}
	var amounts1 = param.AmountList{Amount: "sale",Title: "商品总销量",Tips: "所有商品的总销量",Count: amounts.SaleCount}
	var amounts2 = param.AmountList{Amount: "favor",Title: "商品总收藏",Tips: "所有商品的总收藏",Count: amounts.FavorCount}
	var amounts3 = param.AmountList{Amount: "inventory",Title: "商品总库存",Tips: "所有商品的总库存",Count: amounts.InventoryCount}
	var amounts4 = param.AmountList{Amount: "saleroom",Title: "商品总销售额",Tips: "所有商品的总销售额",Count: amounts.SaleTotalCount}
	amountList= append(amountList,&amounts1,&amounts2,&amounts3,&amounts4)
	return amountList
}

// 获取商品列表信息
func (this *GoodsDao)CategoryList(queryInfo param.QueryInfo) ([]*model.Category, int64){
	var categories []*model.Category
	this.Table("categories").Limit(queryInfo.Size).Offset(queryInfo.Offset).Find(&categories)
	//this.Table("users").Find(&users)
	if utils.Debug{
		fmt.Printf("goodsdao.go 19 goods= %#v",categories[0])
	}
	var count int64
	this.Table("categories").Count(&count)
	return categories,count
}